<template>
	<view class="boxs">
		<view class="ifobox">
			<view class="leftbox">
				收货人：
			</view>
			<view class="rigbox">
				<input type="text" v-model="obj.name" />
			</view>
		</view>
		<view class="ifobox">
			<view class="leftbox">
				联系电话：
			</view>
			<view class="rigbox">
				<input type="text" v-model="obj.phone" />
			</view>
		</view>
		<view class="ifobox">
			<view class="leftbox">
				所在地址：
			</view>
			<view class="rigbox" @click="show = true">
				<input type="text" v-model="obj.site" disabled="false" />
				<view class="angle"></view>
			</view>
		</view>
		<view class="ifobox">
			<view class="leftbox">
				详细地址：
			</view>
			<view class="rigbox">
				<input type="text" v-model="obj.address" />
			</view>
		</view>
		<view class="radiobox">
			<u-radio-group v-model="value" @change="radioGroupChange">
				<u-radio shape="square" active-color="#ff3333" v-for="(item, index) in list" :key="index"
					:name="item.name" :disabled="item.disabled">
					设置为默认地址
				</u-radio>
			</u-radio-group>
		</view>

		<view class="savebox" @click="save" v-if="flag=='a'">
			保存
		</view>

		<view class="btnbox" v-else>
			<view class="delbox" @click="delsite">
				删除
			</view>
			<view class="savebox" @click="save">
				保存
			</view>
		</view>
		<!-- 地区选择器 -->
		<u-picker v-model="show" mode="region" @confirm="confirm" confirm-color="#ff3333"></u-picker>
	</view>
</template>

<script>
	import {
		changeaddress,
		getaddressinfo,
		deleteaddress
	} from "../../api/api/index.js"
	export default {
		data() {
			return {
				obj: {
					name: "",
					phone: "",
					site: "",
					address: ""
				},
				id: "",
				is_default: 0,
				county: null,
				list: [{
					name: '1',
					disabled: false
				}],
				value: '0',
				show: false,
				flag: null
			}

		},
		methods: {
			confirm(e) {
				this.county = e.area.value
				this.obj.site = e.province.label + e.city.label + e.area.label
			},
			radioGroupChange(e) {
				console.log(e)
				if (e == 1) {
					this.is_default = 1
				} else {
					this.is_default = 0
				}
			},
			// 保存
			save() {
				let data = {
					id: this.id,
					name: this.obj.name,
					phone: this.obj.phone,
					address: this.obj.address,
					county: this.county,
					is_default: this.is_default
				}
				uni.showLoading({
					title: "保存中"
				})
				changeaddress(data).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none",
						duration: 2000
					})
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				})
			},
			//删除
			delsite() {
				let that = this
				uni.showLoading({
					title: "加载中"
				})
				deleteaddress({
					id: this.id
				}).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none",
						duration: 2000
					})
					setTimeout(function() {
						uni.navigateBack({
							delta:1
						})
					}, 2000)
				})
			}
	},
	onLoad(e) {

		this.flag = e.id
		if (e.id != "a") {
			getaddressinfo({
				id: e.id
			}).then(res => {
				this.obj.name = res.data.name
				this.obj.phone = res.data.phone
				this.obj.address = res.data.address
				this.obj.site = res.data.city_name
				this.id = e.id
			})
		}
	}
	}
</script>

<style scoped lang="scss">
	.boxs {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		top: 0;
		background-color: #FFFFFF;
		padding: 61rpx 20rpx;
		box-sizing: border-box;

		.ifobox {
			display: flex;
			margin-bottom: 50rpx;

			.leftbox {
				font-size: 26rpx;
				color: #282828;
				font-family: MicrosoftYaHei;
			}

			.rigbox {
				border-bottom: 2rpx solid #cecece;
				display: flex;
				justify-content: space-between;
				width: 580rpx;
				align-items: center;

				input {
					margin-bottom: 10rpx;
					width: 500rpx;
				}

				.angle {
					width: 15rpx;
					height: 15rpx;
					border-top: 1px solid #000;
					border-left: 1px solid #000;
					transform: rotate(135deg)
				}
			}
		}

		.radiobox {
			margin-top: 119rpx;
		}

		.btnbox {
			display: flex;
			align-items: center;
			justify-content: space-around;
			margin-top: 94rpx;
			margin-left: 73rpx;

			.delbox {
				width: 260rpx;
				height: 70rpx;
				background-color: #ff7e65;
				border-radius: 35rpx;
				line-height: 70rpx;
				text-align: center;
				color: #FFFFFF;
				font-family: MicrosoftYaHei;
				font-size: 26rpx;
			}

			.savebox {
				width: 260rpx;
				height: 70rpx;
				background-color: #ff3333;
				border-radius: 35rpx;
				line-height: 70rpx;
				text-align: center;
				color: #FFFFFF;
				font-family: MicrosoftYaHei;
				font-size: 26rpx;
			}
		}

		.savebox {
			width: 586rpx;
			height: 70rpx;
			background-color: #ff3333;
			border-radius: 35rpx;
			text-align: center;
			line-height: 70rpx;
			color: #FFFFFF;
			font-family: MicrosoftYaHei;
			font-size: 26rpx;
			margin: 85rpx auto;
		}
	}
</style>
